package com.lai.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lai.common.valid.AddGroup;
import com.lai.common.valid.UpdateGroup;
import com.lai.common.valid.UpdateStatusGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lai.gulimall.product.entity.BrandEntity;
import com.lai.gulimall.product.service.BrandService;
import com.lai.common.utils.PageUtils;
import com.lai.common.utils.R;


/**
 * 品牌
 *
 * @author laiciqi
 * @email 602469238@qq.com
 * @date 2021-11-06 20:30:09
 */
@RestController
@RequestMapping("product/brand")
@Api(value = "品牌模块",tags = "品牌模块")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查看全部品牌列表")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{brandId}")
    @ApiOperation(value = "根据id获取对应品牌信息")
    @ApiImplicitParam(name = "brandId", value = "品牌id", required = true, dataType = "Long")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    @GetMapping("/infos")
    @ApiOperation(value = "获取多个品牌信息")
    @ApiImplicitParam(name = "brandIds", value = "品牌集合",required = true)
    public R info(@RequestParam("brandIds") List<Long> brandIds){
        List<BrandEntity> brands = brandService.getBrandsByIds(brandIds);
        return R.ok().put("brands",brands);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存品牌")
    @ApiImplicitParam(name = "brand", value = "品牌实体类", required = true, dataType = "BrandEntity")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){
        /*if(result.hasErrors()){
            Map<String, String> map = new HashMap<>();
            //1、获取校验的错误结果
            result.getFieldErrors().forEach((item) ->{
                //2、FieldError 获取到错误提示
                String message = item.getDefaultMessage();
                //3、获取到错误的属性的名字
                String field = item.getField();
                map.put(field,message);
            });
            return R.error(400,"提交的数据不合法").put("data",map);
        }else{
            brandService.save(brand);
        }*/
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改品牌细节")
    @ApiImplicitParam(name = "brand", value = "品牌实体类", required = true)
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update/status")
    @ApiOperation(value = "根据品牌id修改状态")
    @ApiImplicitParam(name = "brand", value = "品牌实体类", required = true)
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除品牌")
    @ApiImplicitParam(name = "brandIds",value = "需要删除的所有id", required = true)
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
